<?php
/**
 * Load City Streets from an external source 
 * it could be a web site or the local computer
 */

set_time_limit(Xview::TIMEOUT_VERY_LONG);
// Screen
use Fox\Common\Localization\City\Street;
use Fox\Common\Localization\City;
use Fox\Html\Input;
use Fox\Html\Screen;
$screen = new Screen("sSelect","extract");
$block = new Block("bSelect","Select Cities");
$block->add(new TextArea("postal_code"))->SetISize(9000);
$block->add(new CheckBox("commit"));
$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$grid = new Fox\Grid("gResult");
	$grid->setFieldCat(array(array("msgtp","type"),"name","ilink",array("ilink","olink"),"postal_code","nbstreets","msgtx"));
	$inputs = $screen->get();
	if(!Xview::instance()->errors())
	{
		$gridData = null;
		
		if(!empty($inputs['postal_code']))
		{
			$sql = null;
			$zipcodes = preg_split("/,/", $inputs['postal_code']);
			foreach($zipcodes as $zipcode)
			{
				if(empty($sql))
					$sql = " postal_code LIKE '$zipcode%' ";
				else
					$sql .= "OR postal_code LIKE '$zipcode%' ";
			}
			$fragments[] = new Fox\Dao\SqlFragment($sql);
		}
		$fragments[] = new Fox\Dao\SqlFragment("streetid IS NULL AND disable = 0");
		$fragments[] = new Fox\Dao\SqlFragment("postal_code > ? ",array(30000));
		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		// Load Cities
		$finder = new City();
		$cities = $finder->find($fragment,array("fullname" => false),array(City::$JOIN_STREETS),$inputs['dbcnt'],true);
		
		foreach($cities as $city)
		{
			// Load current streets
			$streets = $city->streets();
			if(count($streets) > 0)
			{
				$gridData[] = array("type" => "I",
									"name" => $city->name(),
									"postal_code" => $city->postal_code,
									"nbstreets" => count($streets),
									"msgtx" => " already imported");
				// Check if the file exist in the disk and change its name
				$filename = "Liste des rues - ".$city->fullname." (".$city->postal_code.").htm";
				$dirname = "D:/Ancien E/EasyPHP-5.3.8.1/www/cities/";
				//$filename = preg_replace("/ /","%20",$filename);
				if(file_exists($dirname.$filename))
				{
					rename($dirname.$filename,$dirname."treated/".$filename);
				}
				continue;
			}
			$nbstreets = 0;
			
			//$ilink = "http://webcache.googleusercontent.com/search?sclient=psy-ab&hl=fr&biw=1280&bih=620&source=hp&q=cache%3Ahttp%3A%2F%2Fwww.meilleursagents.com%2Fprix-immobilier%2F".$city->keyname."-".$city->postal_code."%2Frues%2F&pbx=1&oq=cache%3Ahttp%3A%2F%2Fwww.me";
			//$original = "http://www.meilleursagents.com/prix-immobilier/".$city->keyname."-".$city->postal_code."/rues/";
			$ilink = $original = null;
			//http://offres.numericable.fr/eligibilite/test?codePostal=93260
			//
			//$ilink = "http://127.0.0.1/cities/cities.htm";
			//$ilink = "http://127.0.0.1/cities/Liste des rues - ".$city->keyname." (".$city->postal_code.").htm";
			//$ilink = preg_replace("/ /","%20",$ilink);
			//$filename = "Liste des rues - ".$city->fullname." (".$city->postal_code.").htm";
			//$dirname = "D:/Ancien E/EasyPHP-5.3.8.1/www/cities/";
			//$ilink = $dirname.$filename;
			/*
			$html = @file_get_html($ilink);
			
				foreach($html->find('div.module') as $key => $elm)
				{
					
					foreach($elm->find('a') as $parts)
					{
						$uglyname = utf8_decode(trim($parts->plaintext));
						if(strlen($uglyname) > 1)
						{
							$streettype = null;
							 
							$steetParts = preg_split("/\(/",$uglyname);
							$streetname = $steetParts[0];
							if(isset($steetParts[1]))
							{
								$streettype = preg_replace("/\)/","",$steetParts[1]);
							}
							$street = new Street();
							$street->setFields(array("fullname" => $streetname,"type" => $streettype));
							$nbstreets++;
							$city->addStreet($street);
						}
					}
				}
				
			*/
				//$ilink = "http://127.0.0.1/cities/test.htm";
				$codeiso = sprintf("%05s",$city->iso_code); 
				$city->iso_code = null;
				$city->iso_code = $codeiso;
				$postal_code = sprintf("%05s",$city->postal_code); 
				$city->postal_code = null;
				$city->postal_code = $postal_code;
				$ilink = "http://offres.numericable.fr/eligibilite/test?codePostal=".$city->postal_code."&codeInsee=".$city->iso_code;
				$html = file_get_html($ilink);
				//var_dump($html);
				/*
				foreach($html->find('body') as $key => $elm)
				{
					
					$data = json_decode(utf8_decode(trim($elm->plaintext)));
					foreach($data->results as $result)
					{
						$street = new Street();
						$street->setFields(array("fullname" => $result->nomRue,"type" => null,"rivoli" => $result->codeRivoli));
						$nbstreets++;
						$city->addStreet($street);
					}
				}
				*/
				
				$data = json_decode(utf8_decode(trim($html->doc)));
				if(!empty($html->doc) && empty($data))
				{
					push_msg("E","Parsing Source Issue on $city->postal_code ");
						$gridData[] = array("type" => "E",
											"name" => $city->name(),
											"ilink" => $ilink,
											"postal_code" => $city->postal_code,
											"nbstreets" => $nbstreets,
											"msgtx" => "Parsing Source Issue");
					continue;
				}
				foreach($data->results as $result)
				{
					if(!isset($result->nomRue))
					{
						push_msg("E","Error on $city->postal_code ");
						$gridData[] = array("type" => "E",
											"name" => $city->name(),
											"ilink" => $ilink,
											"postal_code" => $city->postal_code,
											"nbstreets" => $nbstreets,
											"msgtx" => "Source Issue");
						$nbstreets = 0;
						break;
					}
					$street = new Street();
					$street->setFields(array("fullname" => $result->nomRue,"type" => null,"rivoli" => $result->codeRivoli));
					$nbstreets++;
					
					$city->addStreet($street);
				}
			
			
				if ($nbstreets == 0)
				{
					$gridData[] = array("type" => "W",
										"name" => $city->name(),
										"ilink" => $ilink,
										"olink" => $original,
										"postal_code" => $city->postal_code,
										"nbstreets" => $nbstreets,
										"msgtx" => " no valid source file");
			
				}
				else
				{
					try {
						if($inputs['commit'])
						{
							$city->update();
						}
						$gridData[] = array("type" => "I",
											"name" => $city->name(),
											
											"postal_code" => $city->postal_code,
											"nbstreets" => $nbstreets,
											"msgtx" => "streets have been added");
						if(isset($filename))
						{
							rename($dirname.$filename,$dirname."treated/".$filename);
						}
						
					}
					catch (Fox\Object\Exception\Save $e)
					{
						$gridData[] = array("type" => "E",
											"name" => $city->name(),
											"ilink" => $ilink,
											"postal_code" => $city->postal_code,
											"nbstreets" => $nbstreets,
											"msgtx" => $e->getMessage());
					}
				}
		}
		$grid->setDatas($gridData);
	}
}
echo $screen->display();
if(isset($grid))
	echo $grid->display();